package com.spider.silence.main;

import com.spider.silence.downloader.TencentXinWenDownloader;
import com.spider.silence.downloader.WangYiXinWenDownloader;
import com.spider.silence.pool.TencentXinWenDownloaderPool;
import com.spider.silence.pool.WangYiXinWenDownloaderPool;
import com.spider.silence.utils.PropertiesUtil;
import com.spider.silence.worker.TencentXinWenWorker;
import com.spider.silence.worker.WangYiXinWenWorker;
import org.apache.log4j.PropertyConfigurator;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Linbo Hu on 2017/7/19.
 * 腾讯新闻爬虫主方法
 *
 */
public class TencentXinWenMain {

    public static void main(String[] args) throws IOException {
        //初始化日志环境
        PropertyConfigurator.configure(PropertiesUtil.getLogPro());
        //初始化下载池
        TencentXinWenDownloaderPool downloaderPool = TencentXinWenDownloaderPool.getInstance();
        new Thread(new Runnable() {
            @Override
            public void run() {
                //获取一个下载器
                TencentXinWenDownloader downloader = null;
                try {
                    downloader = downloaderPool.fetchDownLoader(1000);
                    downloader.generateTask();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    if(downloader != null) {
                        downloaderPool.releaseDownLoader(downloader);
                    }
                }
            }
        }).start();
        ExecutorService pool =  Executors.newFixedThreadPool(3000);
        //初始化中国新闻网四川频道的Worker并且添加到线程池中
        for(int i = 0; i<Integer.valueOf(PropertiesUtil.getVal("Tencent.workCount")); i++){
            pool.execute(new TencentXinWenWorker());
        }

    }



}
